package dream.web;

import cn.hutool.core.util.StrUtil;
import dream.pojo.User;
import dream.service.ArticleService;
import dream.util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/addart")
public class AddBlogServlet extends HttpServlet {
    private ArticleService articleService = new ArticleService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User userInfo= SessionUtils.getLoginUser(req);
        if (userInfo != null) {
            //1.得到并验证参数
            String title=req.getParameter("title");
            String context=req.getParameter("context");

            if(!StrUtil.isBlank(title) && !StrUtil.isBlank(context)){
                articleService.InsertBlog(title, context, userInfo.getId());
                resp.getWriter().println(userInfo.getId());
            }

        }
    }
}
